#include <stdio.h>
#include <stdlib.h>

#define MAX_SIZE 20

//宏定义树结构中数据类型
typedef char ElemType;

//结点结构
typedef struct Snode {
    ElemType data;
    int parent;
}PNode;

//树结构
typedef struct {
    PNode tnode[MAX_SIZE];
    int num;
}PTree;

PTree initNode(PTree tree) {
    tree.num = 10;
    tree.tnode[0].data = 'R';
    tree.tnode[1].data = 'A';
    tree.tnode[2].data = 'B';
    tree.tnode[3].data = 'C';
    tree.tnode[4].data = 'D';
    tree.tnode[5].data = 'E';
    tree.tnode[6].data = 'F';
    tree.tnode[7].data = 'G';
    tree.tnode[8].data = 'H';
    tree.tnode[9].data = 'I';

    tree.tnode[0].parent = -1;
    tree.tnode[1].parent = 0;
    tree.tnode[2].parent = 0;
    tree.tnode[3].parent = 0;
    tree.tnode[4].parent = 1;
    tree.tnode[5].parent = 1;
    tree.tnode[6].parent = 3;
    tree.tnode[7].parent = 6;
    tree.tnode[8].parent = 6;
    tree.tnode[9].parent = 6;

    return tree;
}

void findParent(PTree tree) {
    char a;
    int isFind = 0;
    printf("请输入要查询的节点值：R或A~I\n");
    //a = getchar();
    scanf("%c", &a);
    for (int i = 0; i < tree.num; i++)
    {
        /* code */
        if(tree.tnode[i].data == a) {
            isFind = 1;
            int addr = tree.tnode[i].parent;
            printf("%c的父节点值为%c，存储位置下标为%d", a, tree.tnode[addr].data, addr);
            break;
        }
    }
    if(isFind == 0) {
        printf("树中无此节点");
    }
    
}

int main(int argc, char * argv[]) {
    PTree tree;
    //初始化树
    for(int i = 0; i < MAX_SIZE; i++) {
        tree.tnode[i].data = " ";
        tree.tnode[i].parent = 0;
    }
    tree = initNode(tree);
    
    printf("层次遍历1: \n");
    printf("%d", tree.num);
    for(int i = 0; i < tree.num; i++) {
        printf("%c ", tree.tnode[i].data);
        printf("%d\n", tree.tnode[i].parent);
    }
    findParent(tree);
    return 0;
}